﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
using AddressBook.Entity;

namespace AddressBook.DAO
{
    public class FeedbackDAO
    {
        public int InsertData(Feedback model)
        {
            try
            {
                StringBuilder sbColumn = new StringBuilder("insert into Feedback(");
                StringBuilder sbValues = new StringBuilder(" )values(");
                PropertyInfo[] properties = model.GetType().GetProperties();
                SqlParameter[] parameters = new SqlParameter[properties.Length - 1];
                int i = 0;

                foreach (System.Reflection.PropertyInfo info in model.GetType().GetProperties())
                {
                    if (info.Name == "FeedbackID") { continue; }
                    object value = info.GetValue(model, null);
                    sbColumn.AppendFormat("{0},", info.Name);
                    sbValues.AppendFormat("@{0},", info.Name);
                    if (value != null)
                    {
                        parameters[i] = new SqlParameter("@" + info.Name, value);
                    }
                    else
                    {
                        parameters[i] = new SqlParameter("@" + info.Name, DBNull.Value);
                    }
                    i++;
                }
                string cmd = sbColumn.Remove(sbColumn.Length - 1, 1).Append(sbValues.Remove(sbValues.Length - 1, 1).ToString()).ToString() + ")";
                return Globle.ExecuteNonQuery(cmd, CommandType.Text, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
